package dataStructure;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class TreeFile extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
		TreeFile tf = new TreeFile();
		tf.init();
	}

	/**
	 * 初始化UI界面
	 */
	private void init() {
		this.setTitle("JTree 结构");
		this.setSize(400, 400);
		this.setLayout(new FlowLayout());
		JTree tree = createTree();
		this.add(tree);
		tree.setComponentPopupMenu(createPopMenu(tree));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	/**
	 * 创建JTree类型的结构树
	 * 
	 * @return [JTree]tree
	 */
	private JTree createTree() {
		JTree tree = new JTree(); // 创建默认树
		// 创建一个根节点
		DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();

		// 根节点显示的标题为设定对象的toString()值
		rootNode.setUserObject("根目录");
		for (int i = 0; i < 5; ++i) { // 五个子目录
			DefaultMutableTreeNode teamNode = new DefaultMutableTreeNode();
			teamNode.setUserObject("子目录" + i);
			rootNode.add(teamNode);
			for (int j = 0; j < 3; ++j) {
				DefaultMutableTreeNode userNode = new DefaultMutableTreeNode();
				userNode.setUserObject("文件" + j);
				teamNode.add(userNode); // 将文件节点添加到目录下
			}
			// 创建树的Model对象
			DefaultTreeModel dm = new DefaultTreeModel(rootNode);
			tree.setModel(dm);
		}
		return tree;
	}

	/**
	 * 添加响应菜单
	 * 
	 * @param tree 文件树对象
	 * @return 响应式菜单
	 */
	private JPopupMenu createPopMenu(final JTree tree) {
		// 弹出菜单对象
		JPopupMenu popMenu = new JPopupMenu();
		// 创建文件菜单下的菜单项
		JMenuItem itemOpen = new JMenuItem("Add");
		itemOpen.setActionCommand("add");
		JMenuItem itemDelete = new JMenuItem("Delete");
		itemDelete.setActionCommand("delete");
		JMenuItem itemModify = new JMenuItem("Modify");
		itemModify.setActionCommand("modify");
		ActionListener listener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				treeMenuAction(e, tree);
			}
		};
		// 菜单项加上监听器
		itemDelete.addActionListener(listener);
		itemModify.addActionListener(listener);
		itemOpen.addActionListener(listener);
		// 添加到菜单对象上
		popMenu.add(itemDelete);
		popMenu.add(itemModify);
		popMenu.add(itemOpen);
		return popMenu;
	}

	private void treeMenuAction(ActionEvent e, JTree tree) {
		String command = e.getActionCommand();
		TreePath treePath = tree.getSelectionPath(); // 获取树上的选取路径
		if (null != treePath) {
			// 获取树上路径最后的节点
			// 得到的选中节点都是DefaultMutableTreeNode 的对象
			DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
			Object userObj = selectedNode.getUserObject();
			JOptionPane.showMessageDialog(this, command + " you choose " + userObj);
		} else {
			JOptionPane.showMessageDialog(this, "please choose on the tree");
		}
	}
}
